package com.wgod.demo.business.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wgod.demo.business.entity.LogEntity;
import com.wgod.demo.business.mapper.LogMapper;
import com.wgod.demo.common.constant.ErrorConstant;
import com.wgod.demo.common.exception.BusinessException;

import com.wgod.security.common.biz.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  请求日志
 * Created by WGod on 2018/4/29.
 */
@Service
public class LogService extends BaseService<LogMapper,LogEntity> {


    public void addLog(String action, String data, String ip, Integer authorId) {
        LogEntity logEntity = new LogEntity();
        logEntity.setAuthorId(authorId);
        logEntity.setIp(ip);
        logEntity.setData(data);
        logEntity.setAction(action);
        mapper.addLog(logEntity);
    }

    public void deleteLogById(Integer id) {
        if (null == id)
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        deleteLogById(id);
    }

    public PageInfo<LogEntity> getLogs(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<LogEntity> logs = mapper.getLogs();
        PageInfo<LogEntity> pageInfo = new PageInfo<>(logs);
        return pageInfo;
    }
}
